<template>
    <div class="module-page" v-loading="loading">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
            <el-form-item label="网络选择" prop="network">
                <el-radio-group v-model="ruleForm.network" size="small" class="left-btn">
                    <el-radio-button v-for="item in networks" :label="item.value" :key="item.value">{{ item.label }}</el-radio-button>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="模板名称" prop="modelName">
                <el-input :maxlength="100" show-word-limit v-model="ruleForm.modelName"></el-input>
            </el-form-item>
            <el-form-item label="设备分类" prop="deviceType">
                <el-cascader
                        style="width: 100%"
                        v-model="ruleForm.deviceType"
                        :options="deviceTypeList"
                        :props="{
                            value: 'key',
                            label: 'keyName'
                        }"
                        @change="getKnowledgeParam"></el-cascader>
            </el-form-item>
            <el-form-item v-if="ruleForm.deviceType" label="设备指标" prop="knowledgeIds">
                <div class="flex-row">
                    <el-button @click="selectAll" type="text">全选</el-button>
                    <el-button @click="clearAll" type="text">清空选择</el-button>
                </div>
                <el-checkbox-group v-model="ruleForm.knowledgeIds">
                    <el-checkbox v-for="item in knowledgeParam"
                                 :key="item.id"
                                 :label="item.id">
                        <el-popover
                                :open-delay="500"
                                placement="top-start"
                                title="指标说明"
                                trigger="hover">
                            <div>
                                <p>{{item.remarks}}</p>
                            </div>
                            <div slot="reference" style="min-width: 90px">{{ item.paramName }}</div>
                        </el-popover>
                    </el-checkbox>
                </el-checkbox-group>
            </el-form-item>
            <el-form-item label="描述" prop="description">
                <el-input :maxlength="200" show-word-limit type="textarea" v-model="ruleForm.description"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button size="small" @click="cancel">取消</el-button>
                <el-button size="small" type="primary" @click="submitForm">确定</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { networks } from '@/assets/dict'
import {removeSession} from '@/utils/storage';
import { mapState } from 'vuex'

export default {
  name: 'templateEdit',
  data() {
    return {
      networks,
      ruleForm: {
        network: 'JZW',
        modelName: '',
        deviceType: '',
        description: '',
        knowledgeIds: []
      },
      rules: {
        modelName: [
          { required: true, message: '请输入模板名称', trigger: 'blur' }
        ],
        deviceType: [
          { required: true, message: '请选择设备分类', trigger: 'change' }
        ],
        knowledgeIds: [
          { type: 'array', required: true, message: '请至少选择一个指标' }
        ],
        description: [
          { required: false }
        ]
      },
      knowledgeParam: [],
      loading: false,
      currentParam: {
        id: ''
      }
    }
  },
  methods: {
    saveParam() {
      this.$refs.ployfit.updateById()
    },
    selectAll() {
      this.ruleForm.knowledgeIds = this.knowledgeParam.map(item => item.id)
    },
    clearAll() {
      this.ruleForm.knowledgeIds = []
    },
    addModel() {
      this.loading = true
      const {deviceType, ...params} = this.ruleForm
      this.$http.post('/evaluate/addModel', {
        ...params,
        deviceType: deviceType[0],
        deviceTypeTwo: deviceType[1],
        knowledgeIds: this.ruleForm.knowledgeIds.join(',')
      }).then(res => {
        this.$TZmessage.success('新增成功')
        this.$emit('success')
      }).catch(err => {
        this.$TZmessage.error(err.msg || '新增失败')
      }).finally(() => {
        this.loading = false
      })
    },
    getKnowledgeParam() {
      this.loading = true
      this.$http.get('/knowledge/getKnowledgeParam', {
        params: {
          deviceType: this.ruleForm.deviceType[0],
          deviceTypeTwo: this.ruleForm.deviceType[1]
        }
      }).then(res => {
        this.knowledgeParam = res
      }).catch(err => {
        this.$TZmessage.error(err.msg || '获取设备指标失败')
      }).finally(() => {
        this.loading = false
      })
    },
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.addModel()
        } else {
          return false;
        }
      });
    },
    cancel() {
      this.$emit('cancel')
    },
    handleCheckChange(a, b) {
      this.ruleForm.equipment = b.checkedKeys
      this.$refs.ruleForm.validateField('equipment')
    },
    queryInfo() {
      this.$http.get('/evaluate/getModelDetail', {
        params: {
          id: this.tempId
        }
      }).then(res => {
        this.tempInfo = res
      })
    },
  },
  mounted() {
    this.tempId = this.$route.query.tempId
    this.ruleForm.network = this.$route.query.network || 'JZW'
    if (this.$route.query.tempId) {
      this.queryInfo()
    }
  },
  computed: {
    ...mapState('dict', ['deviceTypeList']),
    expandedKeys() {
      return [this.treeData[0].id]
    }
  }
}
</script>

<style lang="scss" scoped>
.module-page {
    padding: 27px;
    width: 800px;
    box-sizing: border-box;
}
</style>